#include "WinManager.h"
#include "iUiNumButton.h"

iUiNumButton::iUiNumButton(void)
{
	m_imgPressed = NULL;
	m_imgNormal = NULL;
	m_bIsClipExtend = FALSE;
	SetRect(&m_rcExtendSize, 0, 0, 0, 0);

	SetButtonType(MZC_BUTTON_NONE);
	EnablePressedHoldSupport(true);
	SetPressedHoldTime(2500);
//	SetButtonMode(MZC_BUTTON_MODE_HOLD);
	m_bNeedDrawNormal = TRUE;
}

iUiNumButton::~iUiNumButton(void)
{
}
/*
void iUiNumButton::PostCommandMessage(int nID, int nNotifyCode, LPARAM lParam)
{
	WinManager *pMan = GetManager();
	if(pMan)
		pMan->PostCommandMessage(MAKEWPARAM(nID, nNotifyCode), lParam);
}
*/
void iUiNumButton::OnPressedHoldTimeup()
{
#ifdef _DEBUG
	printf("iUiNumButton::OnPressedHoldTimeup()\r");
#endif
//	PostCommandMessage(GetID(), 1, 0);
}
void iUiNumButton::PaintWin(HDC hdc, RECT* prcWin, RECT* prcUpdate)
{
	RECT rc = *prcWin;

	if(GetState() == MZCS_BUTTON_PRESSED)
	{
		if((m_imgPressed != NULL))
		{
			RECT rcExtend = rc;
			if(m_bIsClipExtend)
			{
				rcExtend.left -= m_rcExtendSize.left;
				rcExtend.top -= m_rcExtendSize.top;
				rcExtend.right += m_rcExtendSize.right;
				rcExtend.bottom += m_rcExtendSize.bottom;
			}
			m_imgPressed->Draw(hdc, &rcExtend, false, false);
		}

	}
	else
	{
		if((m_imgNormal != NULL))
		{
			m_imgNormal->Draw(hdc, &rc, false, false);	
		}
	}

	for(UINT nPos = 0; nPos < GetText().Length(); nPos++)
	{
		RECT rcNumSign;

		switch(GetText().C_Str()[nPos])
		{
		case '0':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_ZERO]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '1':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_ONE]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '2':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_TWO]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '3':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_THREE]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '4':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_FOUR]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '5':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_FIVE]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '6':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_SIX]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '7':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_SEVEN]->Draw(hdc, &rcNumSign, false, false);
			break;
		case  '8':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_EIGHT]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '9':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_NINE]->Draw(hdc, &rcNumSign, false, false);
			break;
		case '.':
			rcNumSign.left = rc.left + m_rtNum[nPos].left;
			rcNumSign.right = rc.left + m_rtNum[nPos].right;
			rcNumSign.top = rc.top + m_rtNum[nPos].top;
			rcNumSign.bottom = rc.top + m_rtNum[nPos].bottom;
			m_imgNumSign[NUM_SIGN_DOT]->Draw(hdc, &rcNumSign, false, false);
			break;
		case 'k':
		case 'K':
			rcNumSign.left = rc.left + m_rtNum[6].left;
			rcNumSign.right = rc.left + m_rtNum[6].right;
			rcNumSign.top = rc.top + m_rtNum[6].top;
			rcNumSign.bottom = rc.top + m_rtNum[6].bottom;
			m_imgNumSign[NUM_SIGN_KMZ]->Draw(hdc, &rcNumSign, false, false);
			break;
		case 'm':
		case 'M':
			rcNumSign.left = rc.left + m_rtNum[6].left;
			rcNumSign.right = rc.left + m_rtNum[6].right;
			rcNumSign.top = rc.top + m_rtNum[6].top;
			rcNumSign.bottom = rc.top + m_rtNum[6].bottom;
			m_imgNumSign[NUM_SIGN_MHZ]->Draw(hdc, &rcNumSign, false, false);
			break;
		default:
			break;
		}
	}
}

void iUiNumButton::UiSetClipExtend(BOOL bEnable, RECT &rcSize)
{
	m_bIsClipExtend = bEnable;
	m_rcExtendSize = rcSize;

	if(m_bIsClipExtend)
	{
		EnableClipExtend(true);
		
		int nMax = max(rcSize.top, rcSize.right);
		nMax = max(nMax, rcSize.left);
		nMax = max(nMax, rcSize.bottom);

		SetClipExtend(nMax);
	}
}
void iUiNumButton::SetNumSignImg(ImagingHelper *pImag[], RECT pRt[])
{
	for(UINT nIndex = 0; nIndex < MAX_NUM_SIGN; nIndex++)
	{
		m_imgNumSign[nIndex] = pImag[nIndex];
	}
	for(UINT nRt = 0; nRt < 7; nRt++)
	{
		m_rtNum[nRt].left = pRt[nRt].left;
		m_rtNum[nRt].right = pRt[nRt].right;
		m_rtNum[nRt].top = pRt[nRt].top;
		m_rtNum[nRt].bottom = pRt[nRt].bottom;
	}
}